package com.lbb.utils.recycler

import android.view.LayoutInflater
import android.view.ViewGroup
import com.lbb.utils.databinding.LayoutEmptyBinding
import com.lbb.utils.databinding.LayoutErrorBinding
import com.lbb.utils.databinding.LayoutLoadingBinding

abstract class BaseStateAdapter<VH :BaseViewHolder> : BaseAdapter<BaseViewHolder>() {
    var onClickErrorListener: RecyclerListener.OnClickErrorListener? = null

    companion object {
        const val TYPE_CONTENT = 0
        const val TYPE_EMPTY = -1
        const val TYPE_ERROR = -2
        const val TYPE_LOADING = -3
    }

    var state = TYPE_CONTENT
        set(value) {
            field = value
            notifyDataSetChanged()
        }

    override fun getItemViewType(position: Int): Int {
        when (state) {
            TYPE_EMPTY -> return TYPE_EMPTY
            TYPE_ERROR -> return TYPE_ERROR
            TYPE_LOADING -> return TYPE_LOADING
        }
        return getItemType(position)
    }

    open fun getItemType(position: Int): Int {
        return TYPE_CONTENT
    }

    override fun getItemCount(): Int {
        return if (state == TYPE_EMPTY || state == TYPE_ERROR || state == TYPE_LOADING) {
            1
        } else {
            getItemSize()
        }
    }

    abstract fun getItemSize(): Int

    open fun getEmptyHolder(parent: ViewGroup): BaseViewHolder {
        return BaseHolder(LayoutEmptyBinding.inflate(LayoutInflater.from(context), parent, false))
    }

    open fun getErrorHolder(parent: ViewGroup): BaseViewHolder {
        return BaseHolder(LayoutErrorBinding.inflate(LayoutInflater.from(context), parent, false))
    }

    open fun getLoadingHolder(parent: ViewGroup): BaseViewHolder {
        return BaseHolder(LayoutLoadingBinding.inflate(LayoutInflater.from(context), parent, false))
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder {
        when (viewType) {
            TYPE_EMPTY -> {
                return getEmptyHolder(parent)
            }
            TYPE_ERROR -> {
                return getErrorHolder(parent)
            }
            TYPE_LOADING -> {
                return getLoadingHolder(parent)
            }
        }
        return onCreateHolder(parent, viewType)
    }

    abstract fun onCreateHolder(parent: ViewGroup, viewType: Int): BaseViewHolder

    override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
        when {
            getItemViewType(position) == TYPE_EMPTY -> {
                onEmptyHolder(holder, position)
            }
            getItemViewType(position) == TYPE_ERROR -> {
                onErrorHolder(holder, position)
                onClickErrorListener?.let { onClickErrorListener ->
                    holder.itemView.setOnClickListener {
                        state = TYPE_LOADING
                        onClickErrorListener.onClickError(holder.itemView)
                    }

                }
            }
            getItemViewType(position) == TYPE_LOADING -> {
                onLoadingHolder(holder, position)
            }
            else -> {
                onBindHolder(holder as VH, position)
            }
        }
    }


    abstract fun onBindHolder(holder: VH , position: Int)

    open fun onErrorHolder(holder: BaseViewHolder, position: Int) {

    }

    open fun onEmptyHolder(holder: BaseViewHolder, position: Int) {

    }

    open fun onLoadingHolder(holder: BaseViewHolder, position: Int) {

    }


}